package com.homework

import scala.io.StdIn
import scala.util.Random
import scala.util.control.Breaks.break

abstract class GameFunction(){

  def showFist():Int
}

//玩家类
class User(name:String) extends GameFunction() {

  //初始化分数
  var score:Int = 0

  //定义出拳功能
  override def showFist(): Int = {

    var flag = true

    println("请出拳！1.剪刀 2.石头 3.布")
    var fistNum:Int = 0

    //加入while循环，输入错误时重新输入
    while (flag){
      fistNum = StdIn.readInt()
      if (fistNum >= 1 & fistNum <= 3){
        //根据输入的数字判断出拳
        fistNum match {
          case 1 => println(s"$name 出了剪刀")
          case 2 => println(s"$name 出了石头")
          case 3 => println(s"$name 出了布")
          case _ => println("输入错误")
        }
        flag = false
      } else {println("输入错误，请重新输入 1.剪刀 2.石头 3.布")}

    }
    fistNum

  }

}

//电脑类
class Computer(name:String) extends GameFunction(){

  var score:Int = 0

  override def showFist(): Int = {

    //进行1到3之间的随机数字生成作为出拳数字
    var fistNum = Random.nextInt(3)+1

    //判断出拳数字
    fistNum match {
      case 1 => println(s"$name 出了剪刀")
      case 2 => println(s"$name 出了石头")
      case 3 => println(s"$name 出了布")
      case _ => println("输入错误")
    }
    fistNum
  }
}

class Game() {
  //用于接收电脑角色名
  var computerName: String = _
  //用于判断游戏是否继续
  var flag: Char = _
  //用于接收电脑与玩家showfist方法返回的数字
  var userFist: Int = _
  var computerFist: Int = _

  def start(): Unit = {

    println("请选择对战角色：1.刘备 2.关羽 3.张飞")
    var nameNum = StdIn.readInt()
    //根据输入数字判断对战角色
    nameNum match {
      case 1 => computerName = "刘备"
      case 2 => computerName = "关羽"
      case 3 => computerName = "张飞"
      case _ => println("对战角色输入错误")
    }
    println(s"你选择与$computerName 对战！")

    val computer1 = new Computer(computerName)
    val user1 = new User("玩家1")

    println("准备好了吗？ y/n")

    flag = StdIn.readChar()

    while (flag.equals('y')) {

//      user1.showFist()
//      computer1.showFist()

      userFist = user1.showFist()
      computerFist = computer1.showFist()

      if (userFist == 1 && computerFist == 2 || userFist == 2 && computerFist == 3 || userFist == 3 && computerFist == 1) {

        println("玩家胜")
        user1.score += 2
      }
      else if (userFist == 2 && computerFist == 1 || userFist == 3 && computerFist == 2 || userFist == 1 && computerFist == 3) {

        println("电脑胜")
        computer1.score += 2
      } else {

        println("平局")
        computer1.score += 1
        user1.score += 1
      }

      println("是否开始下一轮，y/n")
      flag = StdIn.readChar()
    }

    println("游戏结束")
    println(s"玩家得分：${user1.score}")
    println(s"电脑得分:${computer1.score}")
  }
}
object FingerGuess {

def main(args: Array[String]): Unit = {
  val game = new Game

  game.start()


}

}
